package com.example.lanouhn.bantangapp.activities.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.lanouhn.bantangapp.R;
import com.example.lanouhn.bantangapp.activities.activities.CommunityArticalDynamic;
import com.example.lanouhn.bantangapp.activities.activities.CommunityArticalOriginal;
import com.example.lanouhn.bantangapp.activities.adapters.Adapter_FCommDyDt;
import com.example.lanouhn.bantangapp.activities.contants.DT;
import com.example.lanouhn.bantangapp.activities.contants.DT_Image;
import com.example.lanouhn.bantangapp.activities.contants.FullyLinearLayoutManager;
import com.example.lanouhn.bantangapp.activities.contants.MyRecyclerView;
import com.example.lanouhn.bantangapp.activities.contants.MyScrollview;
import com.example.lanouhn.bantangapp.activities.utils.HttpUtils;
import com.example.lanouhn.bantangapp.activities.utils.JsonUtils_DT;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by lanouhn on 16/7/23.
 * 文章动态
 */
public class Fragment_Comarticle extends Fragment {
    private LinearLayout   ll_dynamic;//关注动态
    private LinearLayout   ll_originality;//新鲜原创
    private LinearLayout   ll_RankingList;//排行榜
    private Adapter_FCommDyDt adapter;
    private SwipeRefreshLayout  swref;
    private List<DT> dataList;//文本

    private MyRecyclerView xRecyclerView;
    private MyScrollview ss;
    //判断网络是否连接
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:

                    adapter= new Adapter_FCommDyDt(getContext(),dataList);
                    xRecyclerView.setAdapter(adapter);


                    break;
                case 0:
                    Toast.makeText(getActivity(),R.string.fail,Toast.LENGTH_SHORT).show();
                    break;
                case 2:
                    //消除下拉刷新
                    xRecyclerView.setAdapter(adapter);
                    swref.setRefreshing(false);
                    break;

            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.community_article_fragment,container,false);
        ll_dynamic= (LinearLayout) view.findViewById(R.id.ll_dynamic);
        ll_originality= (LinearLayout) view.findViewById(R.id.ll_originality);
        ll_RankingList= (LinearLayout) view.findViewById(R.id.ll_RankingList);
        xRecyclerView= (MyRecyclerView) view.findViewById(R.id.xrecyclerview);
        ss = (MyScrollview)view.findViewById(R.id.ss);


        FullyLinearLayoutManager linearLayoutManager =
                new FullyLinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, true);

        linearLayoutManager.setSmoothScrollbarEnabled(true);
        xRecyclerView.setLayoutManager(linearLayoutManager);
        ss.smoothScrollTo(0,0);//scrollview从第一个开始
        swref= (SwipeRefreshLayout) view.findViewById(R.id.swref);
        swref.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(2000);

                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        handler.sendEmptyMessage(2);//标识
                    }
                }).start();
            }
        });

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initLinshener();
        initData();
    }



    //点击监听事件
    private void initLinshener() {
        ll_dynamic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), CommunityArticalDynamic.class);
                startActivity(it);
            }
        });
        ll_originality.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent it = new Intent(getActivity(), CommunityArticalOriginal.class);
                startActivity(it);
            }
        });
        ll_RankingList.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });

    }
    //绑定数据
    private void initData() {

        dataList = new ArrayList<>();
        new Thread(new Runnable() {
            @Override
            public void run() {
                String result = HttpUtils.doGet(DT_Image.DT_LIST_URL);
                dataList = JsonUtils_DT.getdtList(result);
                if (dataList.size() > 0) {
                    Message msg = new Message();
                    handler.sendEmptyMessage(1);

                } else {
                    handler.sendEmptyMessage(0);
                }
            }
        }).start();
    }
}
